package ru.spbu.math.android.cbrf.model;

import java.util.Comparator;

public class RateType {
	
	private StockItem source;
	private StockItem target;
	private String id;

	public String getName() {
		if (target.getCode().length() == 0) {
			return source.getCode();
		} else {
			return source.getCode() + "/" + target.getCode();
		}
	}
	
    public StockItem getSource() {
        return source;
    }

    public StockItem getTarget() {
        return target;
    }
    
    public String getId(){
    	return id;
    }
    
    public RateType(){
    	source = new StockItem();
    	target = new StockItem();
    	id = "";
    }

    public RateType(StockItem source, StockItem target, String id) {
        this.source = source;
        this.target = target;
        this.id = id;
    }
    

  	@Override
    public boolean equals(Object o) {
	
    	if (o == null) {
    		return false;
    	}
    	
    	try {
			RateType rateType = (RateType) o;
			if (this.id.equals(rateType.getId())) {
				return true;
			} else {
				return false;
			}
		} catch (ClassCastException e) {
			return false;
		}    	
    }

    @Override
    public int hashCode() {
    	return toString().hashCode();
    }

    @Override
    public String toString() {
        return id + "/" + source.getCode() + "/" + target.getCode();
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return new RateType(source, target, id);
    }
    
    public static Comparator<RateType> getComparator() {
    	return new RateTypeComparator();
    }
    
    private static class RateTypeComparator implements Comparator<RateType> {

		public int compare(RateType rateType1, RateType rateType2) {
			
			if (rateType1.getSource().getCode().compareTo(rateType2.getSource().getCode()) == 0) {
				return rateType1.getTarget().getCode().compareTo(rateType2.getTarget().getCode());
			} else {
				return rateType1.getSource().getCode().compareTo(rateType2.getSource().getCode());
			}
		}
    }
}
